﻿// -----------------------------------------------------------------------
// Copyright (c) David Kean. All rights reserved.
// -----------------------------------------------------------------------
extern alias pcl;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using pcl::System.Security.Cryptography;
using pcl::System.Security.Cryptography.Adaptation;
using Moq;
using System.IO;

namespace Portable.Security.Cryptography
{
    [TestClass]
    public class HashAlgorithmTests
    {
        [TestMethod]
        public void ComputeHash1_CallsUnderlyingAlgorithmComputeHash1()
        {
            int callCount = 0;
            var underlyingAlgorithm = IHashAlgorithmFactory.OverrideComputeHash((byte[] buffer) => { callCount++; return null; });

            var algorithm = new HashAlgorithm(underlyingAlgorithm);
            algorithm.ComputeHash(new byte[0]);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void ComputeHash2_CallsUnderlyingAlgorithmComputeHash2()
        {
            int callCount = 0;
            var underlyingAlgorithm = IHashAlgorithmFactory.OverrideComputeHash((buffer, offset, count) => { callCount++; return null; });

            var algorithm = new HashAlgorithm(underlyingAlgorithm);
            algorithm.ComputeHash(new byte[0], 1, 1);

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void ComputeHash3_CallsUnderlyingAlgorithmComputeHash3()
        {
            int callCount = 0;
            var underlyingAlgorithm = IHashAlgorithmFactory.OverrideComputeHash((Stream stream) => { callCount++; return null; });

            var algorithm = new HashAlgorithm(underlyingAlgorithm);
            algorithm.ComputeHash(StreamFactory.CreateReadableStream());

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Dispose_CallsUnderlyingAlgorithmDispose()
        {
            int callCount = 0;
            var underlyingAlgorithm = IDisposableFactory.OverrideDispose<IHashAlgorithm>(() => callCount++);

            var algorithm = new HashAlgorithm(underlyingAlgorithm);
            algorithm.Dispose();

            Assert.AreEqual(1, callCount);
        }

        [TestMethod]
        public void Clear_CallsUnderlyingAlgorithmDispose()
        {
            int callCount = 0;
            var underlyingAlgorithm = IDisposableFactory.OverrideDispose<IHashAlgorithm>(() => callCount++);

            var algorithm = new HashAlgorithm(underlyingAlgorithm);
            algorithm.Clear();

            Assert.AreEqual(1, callCount);
        }
    }
}
